package com.ltu.timkor6.gamesite.admin.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ltu.timkor6.gamesite.shared.CompositeAbstract;
import com.ltu.timkor6.gamesite.shared.UserInfo;

public class CompositeUserInfo extends CompositeAbstract
{
	private VerticalPanel errors;
	private FlexTable table;
	private PasswordTextBox textPassword;
	private PasswordTextBox textRePassword;
	private TextBox textEmail;
	private CheckBox checkAdmin;
	private CheckBox checkTestUser;
	
	private Admin admin;
	
	public CompositeUserInfo(Admin admin)
	{
		this.admin = admin;
		
		panel.clear();
		
		// Title
		final Label title = new Label("Admin User Info");
		title.setStyleName("panelHeader");
		panel.add(title);
		
		// Errors
		errors = new VerticalPanel();
		errors.setStyleName("errors");
		panel.add(errors);
		
		// Profile table
		table = new FlexTable();
		panel.add(table);
		
		initWidget(panel);
	}

	public void updateComposite(UserInfo userInfo)
	{
		errors.clear();
		
		Label labelPassword = new Label("Password");
		textPassword = new PasswordTextBox();
		textPassword.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					admin.checkUserInfo();
				}
			}
		});
		
		Label labelRePassword = new Label("Re-Password");
		textRePassword = new PasswordTextBox();
		textRePassword.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					admin.checkUserInfo();
				}
			}
		});
		
		Label labelEmail = new Label("Email");
		textEmail = new TextBox();
		textEmail.setText(userInfo.getEmail());
		textEmail.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					admin.checkUserInfo();
				}
			}
		});
		
		Label labelTestUser = new Label("Test user:");
		checkTestUser = new CheckBox();
		if(userInfo.isTestUser())
		{
			checkTestUser.setValue(true);
		}
		
		Label labelAdmin = new Label("Admin:");
		checkAdmin = new CheckBox();
		if(userInfo.isAdmin())
		{
			checkAdmin.setValue(true);
		}
		
		Button submit = new Button("Update");
		submit.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				admin.checkUserInfo();
			}
		});
		
		table.setWidget(0, 0, labelPassword);
		table.setWidget(0, 1, textPassword);
		table.setWidget(1, 0, labelRePassword);
		table.setWidget(1, 1, textRePassword);
		table.setWidget(2, 0, labelEmail);
		table.setWidget(2, 1, textEmail);
		table.setWidget(3, 0, labelTestUser);
		table.setWidget(3, 1, checkTestUser);
		table.setWidget(4, 0, labelAdmin);
		table.setWidget(4, 1, checkAdmin);
		table.setWidget(5, 1, submit);
	}

	public String getPassword()
	{
		return this.textPassword.getText().trim();
	}

	public String getRePassword()
	{
		return this.textRePassword.getText().trim();
	}

	public String getEmail()
	{
		return this.textEmail.getText().trim();
	}

	public boolean getTestUser()
	{
		return this.checkTestUser.getValue();
	}

	public boolean getAdmin()
	{
		return this.checkAdmin.getValue();
	}

	public void addError(String string)
	{
		this.errors.add(new Label(string));
	}
}
